﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SQLite;
using System.Text;

namespace SweetSQL.Data.Providers
{
    internal sealed class SQLiteConnectionProvider : ConnectionProviderBase
    {
        public SQLiteConnectionProvider(ConnectionParameters parameters)
            : base(parameters)
        { }

        public override void Connect()
        {
            if (!Connected)
            {
                StringBuilder builder = new StringBuilder("Data Source=");
                builder.Append(Parameters.DataSource);
                if (!string.IsNullOrEmpty(Parameters.Password))
                {
                    builder.Append(";Password=");
                    builder.Append(Parameters.Password);
                }
                Util.AppendCustomProperties(builder, Parameters.CustomProperties);

                Connection = new SQLiteConnection(builder.ToString());
                Connection.Open();
                Connected = true;
            }
        }

        public override IDbCommand CreateCommand(string sql)
        {
            return new SQLiteCommand(sql, Connection as SQLiteConnection, Transaction as SQLiteTransaction) { CommandTimeout = CommandTimeout };
        }

        public override IDataParameter CreateParameter(string name, Type type, int index)
        {
            return new SQLiteParameter(GetSqlDbType(type));
        }

        public override object ConvertToClr(Type type, object value)
        {
            if (type == typeof(Guid))
            {
                return new Guid((byte[])value);
            }
            return base.ConvertToClr(type, value);
        }

        public override object ConvertToDatabase(Type type, object value)
        {
            if (type == typeof(Guid))
            {
                return ((Guid)value).ToByteArray();
            }
            return base.ConvertToDatabase(type, value);
        }

        private DbType GetSqlDbType(Type type)
        {
            switch (Type.GetTypeCode(type))
            {
                case TypeCode.Boolean:
                case TypeCode.Byte:
                    return DbType.Byte;
                case TypeCode.Char:
                    return DbType.StringFixedLength;
                case TypeCode.DateTime:
                    return DbType.DateTime;
                case TypeCode.Decimal:
                    return DbType.Decimal;
                case TypeCode.Double:
                    return DbType.Double;
                case TypeCode.Int16:
                    return DbType.Int16;
                case TypeCode.Int32:
                    return DbType.Int32;
                case TypeCode.Int64:
                    return DbType.Int64;
                case TypeCode.SByte:
                    return DbType.SByte;
                case TypeCode.Single:
                    return DbType.Single;
                case TypeCode.String:
                    return DbType.String;
                case TypeCode.UInt16:
                    return DbType.UInt16;
                case TypeCode.UInt32:
                    return DbType.UInt32;
                case TypeCode.UInt64:
                    return DbType.UInt64;
                case TypeCode.DBNull:
                case TypeCode.Empty:
                case TypeCode.Object:
                default:
                    if (type == typeof(Guid))
                    {
                        return DbType.Binary;
                    }
                    return DbType.Object;
            }
        }
    }
}
